package server.demo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
 * 日志工具类
 * 提供统一的日志记录方法
 */
public class LogUtil {
    
    /**
     * 用户操作日志
     */
    public static void logUserAction(Long userId, String action, String details) {
        Logger logger = LoggerFactory.getLogger("USER_ACTION");
        
        // 设置MDC上下文
        MDC.put("userId", String.valueOf(userId));
        MDC.put("action", action);
        
        logger.info("用户操作 - 用户ID: {}, 操作: {}, 详情: {}", userId, action, details);
        
        // 清理MDC
        MDC.remove("userId");
        MDC.remove("action");
    }
    
    /**
     * 业务操作日志
     */
    public static void logBusinessOperation(String module, String operation, String result) {
        Logger logger = LoggerFactory.getLogger("BUSINESS_OPERATION");
        
        MDC.put("module", module);
        MDC.put("operation", operation);
        
        logger.info("业务操作 - 模块: {}, 操作: {}, 结果: {}", module, operation, result);
        
        MDC.remove("module");
        MDC.remove("operation");
    }
    
    /**
     * 异常日志
     */
    public static void logException(String module, String operation, Exception e) {
        Logger logger = LoggerFactory.getLogger("EXCEPTION");
        
        MDC.put("module", module);
        MDC.put("operation", operation);
        
        logger.error("异常发生 - 模块: {}, 操作: {}, 异常: {}", module, operation, e.getMessage(), e);
        
        MDC.remove("module");
        MDC.remove("operation");
    }
    
    /**
     * 性能日志
     */
    public static void logPerformance(String operation, long duration) {
        Logger logger = LoggerFactory.getLogger("PERFORMANCE");
        
        MDC.put("operation", operation);
        MDC.put("duration", String.valueOf(duration));
        
        if (duration > 3000) { // 超过3秒记录为警告
            logger.warn("性能警告 - 操作: {}, 耗时: {}ms", operation, duration);
        } else {
            logger.info("性能统计 - 操作: {}, 耗时: {}ms", operation, duration);
        }
        
        MDC.remove("operation");
        MDC.remove("duration");
    }
    
    /**
     * 数据库操作日志
     */
    public static void logDatabaseOperation(String table, String operation, String details) {
        Logger logger = LoggerFactory.getLogger("DATABASE");
        
        MDC.put("table", table);
        MDC.put("operation", operation);
        
        logger.debug("数据库操作 - 表: {}, 操作: {}, 详情: {}", table, operation, details);
        
        MDC.remove("table");
        MDC.remove("operation");
    }
    
    /**
     * 安全相关日志
     */
    public static void logSecurity(String event, String details, Long userId) {
        Logger logger = LoggerFactory.getLogger("SECURITY");
        
        MDC.put("event", event);
        if (userId != null) {
            MDC.put("userId", String.valueOf(userId));
        }
        
        logger.warn("安全事件 - 事件: {}, 详情: {}, 用户ID: {}", event, details, userId);
        
        MDC.remove("event");
        if (userId != null) {
            MDC.remove("userId");
        }
    }
}